package com.hxgz.steel.service.Impl;

import com.google.common.collect.Maps;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.BackendUserMapper;
import com.hxgz.steel.domain.BackendUser;
import com.hxgz.steel.service.BackendLoginService;
import com.hxgz.steel.util.MD5Util;
import java.util.HashMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class BackendLoginServiceImp implements BackendLoginService {

  @Autowired
  BackendUserMapper backendUserMapper;

  @Override
  public ServerResponse<BackendUser> userLogin(String username, String password) {
    int rst = backendUserMapper.checkUsername(username);
    if (rst == 0) {
      return ServerResponse.createByErrorMessage("用户名不存在");
    }
    String md5pass = MD5Util.MD5EncodeUtf8(password);
    HashMap<Object, Object> map = Maps.newHashMap();
    map.put("username", username);
    map.put("md5pass", md5pass);
    BackendUser backendUser = backendUserMapper.selectLogin(map);
    if (backendUser == null) {
      return ServerResponse.createByErrorMessage("用户密码有误");
    }
    backendUser.setPassword(StringUtils.EMPTY);

    return ServerResponse.createBySuccess("登入成功", backendUser);
  }
}
